/**
 * 
 */
package oj.swing.server;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import oj.jboss.bean.SBean;
import oj.swing.common.DataSSearch;
import oj.swing.common.DataSSearchField;
import oj.util.sUtil.HUtil;

/**
 * @author Omar
 * 
 */
public class OJSwingTableBean extends SBean {

	private Map registros;

	public Map getFieldSearch(String idSearch) throws RemoteException,
			SQLException, Exception {

		Connection con = null;
		try {
			registros = new HashMap();

			con = getConnection();

			String sql = " select description, PHYSICAL_FIELD,f.ID_SEARCH_FIELD,ID_SEARCH,DATA_TYPE, "
					+ " IS_VISIBLE,IS_PRINTABLE,IS_PRINCIPAL "
					+ " from s_search_field f inner join  s_search_field_lang fl on (fl.ID_SEARCH_FIELD = f.ID_SEARCH_FIELD) "
					+ " WHERE id_search =getIdSearch";

			sql = sql.replaceAll("getIdSearch", "'" + idSearch + "'");
			PreparedStatement datos = con.prepareStatement(sql);
			ResultSet rs = datos.executeQuery();

			int pos = 0;
			DataSSearchField value = null;

			try {
				while (rs.next()) {

					value = new DataSSearchField();

					value.setDATA_TYPE(rs.getString("DATA_TYPE"));
					value.setID_SEARCH(rs.getString("ID_SEARCH"));
					value.setID_SEARCH_FIELD(rs.getString("ID_SEARCH_FIELD"));
					value.setDESCRIPTION(rs.getString("description"));

					value.setIS_PRINCIPAL(true);
					value.setIS_PRINTABLE(true);
					value.setIS_VISIBLE(true);

					registros.put(rs.getString("PHYSICAL_FIELD"), value);
					pos++;

				}

				return registros;

			} catch (Exception e) {
				throw e;
			} finally {
				rs.getStatement().close();
				rs.close();
			}
		} finally {
			con.close();

		}

	}

	public DataSSearch getDataSearch(String idSearch) throws RemoteException,
			SQLException, Exception {

		HUtil hUtil = new HUtil();
		Connection con = getConnection();
		DataSSearch search = null;
		try {
			String sql = " SELECT  ID_SEARCH,NAME,TITLE, "
					+ " PHYSICAL_TABLE,CONDITION AS CONDITIONN, SEARCH_QUERY "
					+ " FROM S_SEARCH "
					+ " WHERE ID_SEARCH= gteId AND IS_DELETED='N'";

			sql = sql.replaceAll("gteId", hUtil.replParam(idSearch));

			PreparedStatement datos = con.prepareStatement(sql);
			ResultSet rs = datos.executeQuery();

			try {
				if (rs.next()) {
					search = new DataSSearch();

					search.setCONDITIONN(hUtil.replParam(rs
							.getString("CONDITIONN")));
					search.setID_SEARCH(hUtil.replParam(rs
							.getString("ID_SEARCH")));
					search.setNAME(hUtil.replParam(rs.getString("NAME")));
					search.setPHYSICAL_TABLE(hUtil.replParam(rs
							.getString("PHYSICAL_TABLE")));
					search.setSEARCH_QUERY(hUtil.replParam(rs
							.getString("SEARCH_QUERY")));
					search.setTITLE(hUtil.replParam(rs.getString("TITLE")));
				}

			} finally {

			}

		} finally {
			if (con != null)
				con.close();
		}
		return search;

	}
}